﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using BusinessLayer;
using ModelLayer;
using ShopKart.Areas.Admin.Models;
using ShopKart.Common;
using ShopKart.Controllers;

namespace ShopKart.Areas.Admin.Controllers
{
    [AuthUser(Roles = "Admin")]
    public class CategoryController : BaseController
    {
        // GET: Admin/Category
        public ActionResult Index()
        {
            CategoryManager categoryManager = new CategoryManager();
            CategoryVm categoryVm = new CategoryVm()
            {
                Categories = categoryManager.GetAllCategories()
            };
            return View(categoryVm);
        }

        [HttpGet]
        public PartialViewResult GetAllCategories(long? categoryId)
        {
            CategoryManager categoryManager = new CategoryManager();
            CategoryVm categoryVm = new CategoryVm()
            {
                Categories = categoryManager.GetAllCategories()
            };
            return PartialView("_GridCategories", categoryVm);
        }

        private static void FillCategoryVm(CategoryVm categoryVm)
        {
            CategoryManager categoryManager = new CategoryManager();
            categoryVm.Categories = categoryManager.GetAllCategories();
        }

        [HttpGet]
        public PartialViewResult AddUpdateCategory(long? categoryId)
        {
            CategoryManager categoryManager = new CategoryManager();
            CategoryVm categoryVm = new CategoryVm();
            if (categoryId > 0)
            {
                var category = categoryManager.GetById(categoryId.Value);
                categoryVm = Mapper.Map<CategoryVm>(category);
            }
            FillCategoryVm(categoryVm);

            return PartialView("_AddUpdateCategory", categoryVm);
        }

        [HttpPost]
        public ActionResult AddUpdateCategory(CategoryVm categoryVm)
        {
            CategoryManager categoryManager = new CategoryManager();
            FillCategoryVm(categoryVm);

            var allErrors = ModelState.Values.SelectMany(v => v.Errors);
            if (!ModelState.IsValid)
            {
                return PartialView("_AddUpdateCategory", categoryVm);
            }

            var category = Mapper.Map<Category>(categoryVm);
            var msg = categoryVm.CategoryId > 0 ? "Category Updated Successfully" : "Category Added Successfully";
            var result = categoryManager.AddUpdate(category);

            if (result) //Success
            {
                ShowMessage(msg, MessageTypeEnum.Success);
            }
            else //Error
            {
                ShowMessage("Some error occured", MessageTypeEnum.Error);

            }

            return PartialView("_AddUpdateCategory", categoryVm);
        }

        public JsonResult Delete(int categoryId)
        {
            CategoryManager categoryManager = new CategoryManager();
            bool result = categoryManager.Delete(categoryId);

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}